package com.xbsg.protocol.xf;

import com.xbsg.protocol.xf.entiy.*;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class RS485Util {
// 16进制字符串，转10进制字符串，
    public static String covertHex2Ten(String hexString){
        //去除空格
        String str2 = hexString.replaceAll(" ","");
        //string 转 array,
        char[] temp = str2.toCharArray();

        //16进制字符串转10进制字符串
        StringBuffer result = new StringBuffer();
        String str = null;
        for(int i =0;i<temp.length; ){
            str =""+ temp[i]+ temp[i+1] ;
            int inter = Integer.parseInt(str,16);
            result.append(inter+" ");
            i = i +2;

        }

        return result.toString();
    }

    //解析协议
    //00101001 光电感烟火警
    public static String parse_light_alarm(){

        return null;
    }

/*    public static String parse_protocol(String message){

        String[] temps = message.split(" ");
        for(int i =0;i<temps.length;i++){
            if(i==3){
                temps[i]
            }
        }
        return null;
    }*/

    //解析应用数据
    public static AppData parse_app_data(String message){
        AppData appData = null;
        String[] temps = message.split(" ");
        appData = new AppData();
        appData.type_flag = temps[0];
        appData.object_num = temps[1];
        return appData;
    }

    //example heart\part status\
    public static String heart = "40 40 09 00 02 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 02 00 02 09 00 1A 23 23";//ok
    public static String light_warning = "40 40 04 00 02 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 34 00 02 02 01 01 01 4E 30 30 31" +
            "30 31 30 30 31 01 31 32 33 34 35 36 37 38 39 30 31 32 33 34 35 36 37 38 39 30 00 00 00 00 00 00 00 00 00" +
            "00 00 00 35 04 0E 1D 0B 0D AB 23 23";//ok
    public static String test1 ="4040F97202010000000000001400000000000000000000000000038523234040FA72020100000000000014000000000000000000000002000209009023234040FA7202010000000000001400" +
            "4040A37F020100000000000014000000000000000000000002000209004623234040A37F020100000000000014000000000000000000000002000450019023234040A37F020100000000000014000000000000000000000054" +
            "0005500109000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000EC232340"+
            "40A47F020100000000000014000000000000000000000002000209004723234040A47F02010000000000001400000000000000000000000000033D2323";//fail ,control 3
    public static String fuwei ="40 40 02 00 02 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 0C 00 02 04 01 01 01 01 00 3A 03" +
            "0E 1D 0B 0D 9C 23 23";//ok

    public static String input_string = "40 40 0A 00 02 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 34 00 02 02 01 01 01 7B 30 30 31" +
            "30 31 30 32 31 3C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" +
            "00 00 00 02 0C 0E 1D 0B 0D D6 23 23";
    public static String input_string_stop = "40 40 0F 00 02 01 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 34 00 02 02 01 01 01 7B 30 30 31" +
            "30 31 30 32 31 29 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 00 00 00 00 00 00 00 00 00" +
            "00 00 00 20 20 02 05 01 11 90 23 23";

    public static String test ="40401EF40201000000000000140000000000000000000000020002090036232340401EF40201000000000000140000000000000000000000020004500180232340401EF40201000000000000140000000000000000000000540005500109000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000DC2323\n";

/*    public static void main(String[] args) {
        //16进制转10进制
        String result = covertHex2Ten(
                test1);
        //System.out.println(result);
        //10进制转Protocol
        Protocol protocol = Protocol.parseProtocol(result);
        System.out.println(protocol.toString());
        if(protocol.control.equals("2")){
            //上报信息
            AppData appData = AppDataUtil.parseAppData(protocol.yingyong_data);
            printAppData(appData);
            //System.out.println(appData.status_objects.get(0).toStringDesc());
        }

    }*/

    public static FireUploadData getFireUploadData(String hexString){
        //16进制转10进制
        String result = covertHex2Ten(hexString);
        Protocol protocol = new Protocol(result);
        FireUploadData fireUploadData = covertFireUploadData(protocol);

        return fireUploadData;
    }
    //015 转 15
    public static String splitStr(String str){
        return Integer.parseInt(str)+"";
    }

    public static FireUploadData covertFireUploadData(Protocol protocol){
        FireUploadData fireUploadData = new FireUploadData();
        //fireUploadData.setId();
        fireUploadData.setBusSerialNum(splitStr(protocol.yewuliushui));
        fireUploadData.setProtocolVersion(protocol.xieyibanbenhao);
        fireUploadData.setTimeLable(AppDataUtil.parse_time_stamp(protocol.time_stamp));
        fireUploadData.setSourceAddress(protocol.source_address);
        fireUploadData.setTargetAddress(protocol.target_address);
        fireUploadData.setAppDataLength(splitStr(protocol.yingyong_data_length));
        fireUploadData.setControl(protocol.control);

        fireUploadData.setCheckSum(protocol.jiaoyan);

        //发送数据
        if (protocol.control.equals("2")){
            if(protocol.getYingyong_data() != null){
                //上报信息
                AppData appData = AppDataUtil.parseAppData(protocol.yingyong_data);
                fireUploadData.setAppDataFlag(appData.type_flag);
                fireUploadData.setAppDataNum(appData.object_num);
                List<FireUploadDataOboject> fireUploadDataObojects = null;
                if(appData.type_flag.equals("2")){
                    //部件运行状态
                    fireUploadDataObojects = new ArrayList<FireUploadDataOboject>(Integer.parseInt(appData.object_num));
                    for(AppData.StatusInfoObject statusInfoObject:appData.status_objects){
                        //part_status
                        FireUploadDataOboject object = new FireUploadDataOboject();
                        object.setDeviceType(statusInfoObject.device_type);
                        object.setDeviceAddress(statusInfoObject.device_address);
                        object.setPartType(statusInfoObject.part_type);
                        object.setPartAddress(statusInfoObject.get_part_address());
                        object.setPartStatus(statusInfoObject.part_status);
                        object.setPartDesc(statusInfoObject.get_part_desc());
                        object.setTimeLable(AppDataUtil.parse_time_stamp(statusInfoObject.time_label));
                        fireUploadDataObojects.add(object);
                    }
                }else if(appData.type_flag.equals("4")){
                    //设备操作信息
                    fireUploadDataObojects = new ArrayList<FireUploadDataOboject>(Integer.parseInt(appData.object_num));
                    FireUploadDataOboject object = new FireUploadDataOboject();
                    object.setDeviceType(appData.optInfoObject.device_type);
                    object.setDeviceAddress(appData.optInfoObject.device_address);
                    object.setDeviceOptType(appData.optInfoObject.opt_type);
                    object.setOpterNum(appData.optInfoObject.opter);
                    fireUploadDataObojects.add(object);
                    //System.out.println(appData.optInfoObject.toStringDesc());
                }else {
                    //9 通信线路上行测试
                    //System.out.println(appData.type_flag);
                }

                fireUploadData.setFireUploadDataObojectList(fireUploadDataObojects);
            }

        }
        return fireUploadData;
    }

    public static void printAppData( AppData appData){
            System.out.println( appData.type_flag + " " + appData.object_num);
            if(appData.type_flag.equals("9")){
                System.out.println("通信线路上行测试");
            }else if(appData.type_flag.equals("2")){
                System.out.println("部件运行状态");
                for(AppData.StatusInfoObject statusInfoObject:appData.status_objects){
                    //part_status
                    System.out.println(statusInfoObject.toString());
                    System.out.println(statusInfoObject.toStringDesc());
                }
            }else if(appData.type_flag.equals("4")){
                System.out.println("设备操作信息");
                System.out.println(appData.optInfoObject.toStringDesc());
            }else {
                System.out.println(appData.type_flag);
            }
            //System.out.println(appData.status_objects.get(0).toStringDesc());
        }

    //部件报警
/*    public static void main(String[] args) {
        String result = covertHex2Ten(
                light_warning);
        System.out.println(result);
        Protocol protocol = parseProtocol(result);
        System.out.println(protocol.toString());
        AppData appData = AppData.parseAppData(protocol.yingyong_data);
        System.out.println( appData.type_flag + " " + appData.object_num +" " + appData.status_objects.get(0).toString());
        System.out.println(appData.status_objects.get(0).toStringDesc());

    }*/
}
